/**   
 * @title TemplateDaoImpl.java 
 * @package com.hopechart.importutil.dao.impl 
 * @description 持久层接口实现
 * @author daijiang@hopechart.com
 * @date 2016年1月13日 上午10:12:35 
 * @version v1.0.0
 * @copyright 杭州鸿泉数字设备有限公司
 */
package com.hopechart.importutil.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.fastjson.JSONArray;
import com.hopechart.importutil.dao.ITemplateDao;
import com.hopechart.importutil.model.pojo.TemplateEntity;
import com.hopechart.importutil.util.CodeMapUtil;

/**
 * @className TemplateDaoImpl
 * @description TODO
 * @author daijiang@hopechart.com
 * @date 2016年1月13日 上午10:12:35
 * 
 */
public class TemplateDaoImpl implements ITemplateDao {

    private static final Logger log = LoggerFactory.getLogger(TemplateDaoImpl.class);
    
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private String sql = "insert into T_B_FAULTCODE (FAULTID, FAULTCODE, FAULTTYPE, FAULT_DESC, ABOUTPART, PROBLEMS, REASON, SOLUTION, SPN, FMI, ECU_TYPE) " + 
            " values ((select (max(T_B_FAULTCODE.Faultid) + ?) from T_B_FAULTCODE) ,? ,? ,? ,? ,? ,? ,? ,? ,? ,?)";
    
    @Override
    public int batchInsert(final List<TemplateEntity> datas) {
        int[] rs = null;
        try {
            rs = jdbcTemplate.batchUpdate(/*PropertiesUtil.getValue("insert.sql")*/ sql, 
                    new BatchPreparedStatementSetter() {
                @Override
                public int getBatchSize() {
                    return datas.size();
                }

                @Override
                public void setValues(PreparedStatement stms, int i)
                        throws SQLException {
                    TemplateEntity entity = datas.get(i);
                    stms.setInt(1, i + 1);
                    stms.setString(2, entity.getFaultCode());
                    stms.setInt(3, CodeMapUtil.getFaultType(entity.getFaultType()));
                    stms.setString(4, entity.getFaultDesc());
                    stms.setString(5, entity.getAboutPart());
                    stms.setString(6, entity.getProblems());
                    stms.setString(7, entity.getReason());
                    stms.setString(8, entity.getSolution());
                    stms.setString(9, entity.getSpn());
                    stms.setString(10, entity.getFmi());
                    stms.setInt(11, CodeMapUtil.getEcu(entity.getEcuType()));
                }
            });
        } catch (RuntimeException e) {
            log.error("批量故障码数据异常！", e);
            log.info(JSONArray.toJSONString(rs));
        }
        return rs == null ? 0 : rs.length;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
